package com.zbkj.crmeb.store.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crmeb.core.common.CommonResult;
import com.common.PageParamRequest;
import com.constants.Constants;
import com.crmeb.core.exception.CrmebException;
import com.github.pagehelper.PageHelper;
import com.utils.DateUtil;
import com.zbkj.crmeb.store.model.SupplierDiscount;
import com.zbkj.crmeb.store.dao.SupplierDiscountDao;
import com.zbkj.crmeb.store.request.AddDiscountRequest;
import com.zbkj.crmeb.store.service.SupplierDiscountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuzp
 * @since 2021-09-25
 */
@Service
public class SupplierDiscountServiceImpl extends ServiceImpl<SupplierDiscountDao, SupplierDiscount> implements SupplierDiscountService {

    @Autowired
    SupplierDiscountDao supplierDiscountDao;

    @Override
    public List<SupplierDiscount> getDiscountList(PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        return supplierDiscountDao.getDiscountList();
    }

    /**
     * 创建折扣
     * @param request
     * @return
     */
    @Override
    public boolean addDiscount(AddDiscountRequest request) {
        //校验时间
        checkTime(request);
        //查询该供应商进行中和未开始的时间段
        List<SupplierDiscount> discountList = supplierDiscountDao.selectList(Wrappers.<SupplierDiscount>lambdaQuery().eq(SupplierDiscount::getSupplierId, request.getSupplierId()));
        checkDiscount(request, discountList);
        SupplierDiscount discount = new SupplierDiscount();
        BeanUtil.copyProperties(request,discount,"id");
        discount.setCreateTime(new Date())
                .setUpdateTime(new Date());
        return save(discount);
    }

    public void checkTime(AddDiscountRequest request) {
        if (request.getStartTime().compareTo(request.getEndTime()) > 0) {
            throw new CrmebException("开始时间不能大于结束时间");
        }
        if (new Date().compareTo(request.getStartTime()) > 0) {
            throw new CrmebException("当前时间不能大于开始时间");
        }
    }

    @Override
    public boolean updateDiscount(AddDiscountRequest request) {
        if(null == request.getId()){
            throw new CrmebException("折扣id不能为空");
        }
        SupplierDiscount supplierDiscount = getById(request.getId());
        if(null == supplierDiscount){
            throw new CrmebException("不存在该条折扣");
        }
        if(new Date().compareTo(supplierDiscount.getEndTime()) > 0){
            throw new CrmebException("已结束的则扣不可编辑");
        }else if(new Date().compareTo(supplierDiscount.getStartTime()) < 0){
            checkTime(request);
            BeanUtil.copyProperties(request,supplierDiscount);
        }else {
            if(new Date().compareTo(request.getEndTime()) > 0){
                throw new CrmebException("当前时间不能大于结束时间");
            }
            supplierDiscount.setEndTime(request.getEndTime());
        }
        List<SupplierDiscount> discountList = supplierDiscountDao.selectList(Wrappers.<SupplierDiscount>lambdaQuery().ne(SupplierDiscount::getId,request.getId()).eq(SupplierDiscount::getSupplierId, request.getSupplierId()));
        checkDiscount(request, discountList);
        supplierDiscount.setUpdateTime(new Date());
        return updateById(supplierDiscount);
    }

    public void checkDiscount(AddDiscountRequest request, List<SupplierDiscount> discountList) {
        for (SupplierDiscount discount : discountList) {
            if (DateUtil.isEffectiveDate(request.getStartTime(), discount.getStartTime(), discount.getEndTime()) || DateUtil.isEffectiveDate(request.getEndTime(), discount.getStartTime(), discount.getEndTime())) {
                throw new CrmebException("该时间段已经有则扣,同一时间不可创建多条折扣信息  " + DateUtil.dateToStr(discount.getStartTime(), Constants.DATE_FORMAT) + "--" + DateUtil.dateToStr(discount.getEndTime(), Constants.DATE_FORMAT));
            }
        }
    }

    @Override
    public boolean stopDiscount(Integer id) {
        SupplierDiscount supplierDiscount = getById(id);
        if(null == supplierDiscount){
            throw new CrmebException("不存在该条折扣");
        }
        if(new Date().compareTo(supplierDiscount.getEndTime()) > 0){
            throw new CrmebException("该则扣已结束");
        }else if(new Date().compareTo(supplierDiscount.getStartTime())<0){
            throw new CrmebException("该则扣未开始");
        }else {
            supplierDiscount.setEndTime(new Date())
                    .setUpdateTime(new Date());
        }
        return updateById(supplierDiscount);
    }

    @Override
    public boolean deleteDiscount(Integer id) {
        return removeById(id);
    }
}
